package chapter012;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * BigDecimal 类
 *
 * 参考链接:
 * 空零之旅 [初识Java（Java数字处理类-大数字运算)]  https://blog.csdn.net/js940814/article/details/80210456
 *
 * @author Jack魏
 */
public class Point02 {
    public static void main(String[] args) {
        //实例化一个大数字
        BigInteger bigInteger = new BigInteger("4");
        System.out.println("加法操作：" + bigInteger.add(new BigInteger("2")));
        System.out.println("减法操作：" + bigInteger.subtract(new BigInteger("2")));
        System.out.println("乘法操作：" + bigInteger.multiply(new BigInteger("2")));
        System.out.println("除法操作：" + bigInteger.divide(new BigInteger("2")));
        System.out.println("取商操作：" + bigInteger.divideAndRemainder(new BigInteger("3"))[0]);
        System.out.println("取余数操作：" + bigInteger.divideAndRemainder(new BigInteger("3"))[1]);
        System.out.println("做 2 次方操作：" + bigInteger.pow(2));
        System.out.println("取相反数操作：" + bigInteger.negate());

        BigDecimal b1 = new BigDecimal("12.3331111");
        BigDecimal b2 = new BigDecimal("22.666999");

        System.out.println("两个数字相加结果：" + b1.add(b2));
        System.out.println("两个数字相减结果：" + b1.subtract(b2));
        System.out.println("两个数字相乘结果：" + b1.multiply(b2));
        System.out.println("两个数字相除结果：" + b1.divide(b2, BigDecimal.ROUND_HALF_UP));
        System.out.println("两个数字相除，保留小数后 5 位：" + b1.divide(b2, 5, BigDecimal.ROUND_HALF_UP));

    }
}
